#!/usr/bin/python
'''
/*
 * This computer program is the confidential information and proprietary trade
 * secret of VistaraIT LLC.  Possessions and use of this program must  conform
 * strictly to the license agreement between the user and VistaraIT LLC.,  and
 * receipt or possession does not convey any rights to divulge, reproduce,  or
 * allow others to use this program without specific written authorization  of
 * VistaraIT LLC.
 * 
 * Copyright (c) 2014 VistaraIT, Inc. All rights reserved.
 */
'''

import commands
from django.template.loader import render_to_string  # @UnresolvedImport
from xml.dom import minidom


DEBUG = False
def executeCommand(cmd, args=[], ignoreOnError=False):
    for arg in args:
        cmd = cmd + ' ' + str(arg)
    if DEBUG:
        print "Executing command: " , cmd
    try:
        result = commands.getstatusoutput(cmd)
    except Exception as errmsg:
        return 1, 'Exception caught - ' + str(errmsg)

    if DEBUG:
        print "Command output is : %s , %s " % (result[0] >> 8 ,result[1]) 
    if result[0] != 0 and ignoreOnError == False:
        raise Exception("Failed to execute command: " + cmd)
    return result[0] >> 8 , result[1]

def processTemplateTOFile(template, toFile, tempateArgs=None):
    rendered = render_to_string(template, tempateArgs)
    #render the template to work directory
    fd = open(toFile,'w')
    fd.write(rendered)
    fd.close()

def parseUpdateResponse(responseData, parseField):
    nodeVal = ""
    xml = minidom.parseString(responseData)
    elements = xml.getElementsByTagName(parseField)
    if elements:
        element = elements[0]
        nodes = element.childNodes
        nodeVal = nodes.item(0).nodeValue
    return nodeVal
    
